<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      title="字典列表"
      row-key="path"
      :indent="30"
      :request-api="getTableList"
      :columns="columns"
      :pagination="true"
    >
      <template #tableHeader>
        <el-button type="primary" :icon="CirclePlus" @click="openDrawer('新增')">新增字典类型 </el-button>
      </template>
      <template #icon="scope">
        <el-icon :size="18">
          <component :is="scope.row.meta.icon"></component>
        </el-icon>
      </template>
      <template #operation="scope">
        <el-button type="primary" link :icon="Tickets" @click="goDictData(scope.row)"> 字典值 </el-button>
        <el-button type="primary" link :icon="View" @click="openDrawer('查看', scope.row)"> 查看 </el-button>
        <el-button type="primary" link :icon="EditPen" @click="openDrawer('编辑', scope.row)"> 编辑 </el-button>
        <el-button type="primary" link :icon="Delete" @click="remove(scope.row.id)"> 删除 </el-button>
      </template>
    </ProTable>
    <DictTypeDrawer ref="drawerRef" />
  </div>
</template>

<script setup lang="ts" name="menuMange">
import { ref } from "vue";
import { ColumnProps } from "@/components/ProTable/interface";
import { Delete, EditPen, CirclePlus, View, Tickets } from "@element-plus/icons-vue";
import ProTable from "@/components/ProTable/index.vue";
import { DictType, page, save, update, deleteById } from "@/api/system/dict";
import DictTypeDrawer from "./detail.vue";
import { useRouter } from "vue-router";

const router = useRouter();

import { ElMessage } from "element-plus";
const proTable = ref();

const getTableList = async (params: any) => {
  return await page(params);
};
const remove = async id => {
  await deleteById(id);
  ElMessage.success({ message: `删除成功！` });
  proTable.value?.getTableList();
};

const goDictData = (row: Partial<DictType.ReqDictType> = {}) => {
  router.push({ name: "dictDataManage", query: { typeCode: row?.type } });
};

const drawerRef = ref<InstanceType<typeof DictTypeDrawer> | null>(null);

const openDrawer = (title: string, row: Partial<DictType.ReqDictType> = {}) => {
  const params = {
    title,
    isView: title === "查看",
    row: { ...row },
    api: title === "新增" ? save : title === "编辑" ? update : undefined,
    getTableList: proTable.value?.getTableList
  };
  drawerRef.value?.acceptParams(params);
};

// 表格配置项
const columns: ColumnProps[] = [
  { type: "sort", label: "排序", width: 80 },
  { prop: "name", label: "字典名称", align: "left", search: { el: "input" } },
  { prop: "type", label: "字典类型" },
  { prop: "createTime", label: "创建时间" },
  { prop: "operation", label: "操作", width: 250, fixed: "right" }
];
</script>
